 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MusicPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Core core;
        private InputHandlerMouse mouse;
        private InputHandlerKeyboard keyboard;
        private PlayList playList;
        private Key keyPressed;
        private int deck;
        public MainWindow()
        {
            InitializeComponent();

            core = Core.Instance(2);
            core.OnTimeChanged += core_OnTimeChanged;
            core.OnMediaLocationChanged += core_OnMediaLocationChanged;
            core.OnStateChanged += core_OnStateChanged;

            // Loading SongList
            playList = new PlayList();
            playList.OnSongListChange += playList_OnSongListChange;

            core.SetPlaylist(playList);

            selectedCellsGrid.DataContext = playList;

            mouse = new InputHandlerMouse();
            keyboard = new InputHandlerKeyboard();
            core.AddInputHandler(mouse);
            core.AddInputHandler(keyboard);
            playList.AddInputHandler(mouse);
            playList.AddInputHandler(keyboard);
            DefOrder.IsChecked = true;

            //orderTypesList.Items.Add("Default");
            //orderTypesList.Items.Add("Repeat");
            //orderTypesList.Items.Add("Random");
        }

        void playList_OnSongListChange(object sender, EventArgs e)
        {
            this.selectedCellsGrid.ItemsSource = null;
            this.selectedCellsGrid.ItemsSource = playList.Songs;
        }

        void core_OnStateChanged(object sender, PlayerStateArgs e)
        {
            Brush brush = Brushes.Black;

            if (e.State == DriverState.Playing)
            {
                brush = Brushes.Red;
            }
            else if (e.State == DriverState.Paused)
            {
                brush = Brushes.Black;
            }
            else if (e.State == DriverState.Stopped)
            {
                brush = Brushes.Black;
            }

            if (e.SourcePlayer == 0)
            {
                if (e.State == DriverState.Stopped)
                {
                    this.TimePositionLeft.Text = "00:00";
                }

                this.TimePositionLeft.Foreground = brush;
            }
            else if (e.SourcePlayer == 1)
            {
                if (e.State == DriverState.Stopped)
                {
                    this.TimePositionRight.Text = "00:00";
                }

                this.TimePositionRight.Foreground = brush;
            }
        }

        void core_OnMediaLocationChanged(object sender, MediaLocationArgs e)
        {
            if (e.SourcePlayer == 0)
            {
                this.MediaLocationLeft.Text = e.MediaLocation.Substring(e.MediaLocation.LastIndexOf("\\") + 1); ;
            }
            else if (e.SourcePlayer == 1)
            {
                this.MediaLocationRight.Text = e.MediaLocation.Substring(e.MediaLocation.LastIndexOf("\\") + 1); ;
            }

            selectedCellsGrid.ScrollIntoView(selectedCellsGrid.SelectedItem);
        }

        void core_OnTimeChanged(object sender, PlayerTimeChanged e)
        {
            if (e.SourcePlayer == 0)
            {
                this.TimePositionLeft.Text = string.Format("{0:mm\\:ss}", e.Time);
            }
            else if (e.SourcePlayer == 1)
            {
                this.TimePositionRight.Text = string.Format("{0:mm\\:ss}", e.Time);
            }
        }

        //Event handlers for Deck1/Left Deck
        private void PlayButtonLeftDeck(object sender, RoutedEventArgs e)
        {
            
            mouse.HandleCommand(Commands.Play, 0);
        }

        private void StopButtonLeftDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Stop, 0);
        }

        private void PauseButtonLeftDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Pause, 0);
        }

        private void PrevButtonLeftDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Previous, 0);
        }

        private void NextButtonLeftDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Next, 0);
        }

        private void MuteButtonLeftDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Mute, 0);
        }

        private void VolumeChangeLeftDeck(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (mouse != null)
            {
                mouse.ChangeVolume(this.VolumeLeftDeck.Value * 10, 0);
            }
        }

        //Event handlers for Deck2/Right Deck
        private void PlayButtonRightDeck(object sender, RoutedEventArgs e)
        {
            try
            {
                mouse.HandleCommand(Commands.Play, 1);
            }
            catch (FormatNotSupportedException fnse)
            {
                MessageBox.Show(fnse.Message);
            }
            
        }

        private void StopButtonRightDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Stop, 1);
        }

        private void PauseButtonRightDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Pause, 1);
        }

        private void PrevButtonRightDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Previous, 1);
        }

        private void NextButtonRightDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Next, 1);
        }

        private void MuteButtonRightDeck(object sender, RoutedEventArgs e)
        {
            mouse.HandleCommand(Commands.Mute, 1);
        }

        private void VolumeChangeRightDeck(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (mouse != null)
            {
                mouse.ChangeVolume(this.VolumeRightDeck.Value * 10, 1);
            }
        }

        //Event handlers for Playlist
        private void PlayListOrderChanged(object sender, SelectionChangedEventArgs e)
        {
            mouse.OrderChange(e.AddedItems[0].ToString());
        }

        private void NewSongSelected(object sender, RoutedEventArgs e)
        {
            mouse.CurrentSelectionChange(this.selectedCellsGrid.SelectedIndex);
        }

        //Keyboard event handler
        private void KeyboardHandler(object sender, KeyEventArgs e)
        {
            keyPressed = e.Key;

            if (e.KeyboardDevice.Modifiers == ModifierKeys.Control)
            {
                deck = 1;
                keyboard.ProcessKeyPressed(keyPressed, deck);
            }
            else if (e.KeyboardDevice.Modifiers == ModifierKeys.None)
            {
                deck = 0;
                keyboard.ProcessKeyPressed(keyPressed, deck);
            }
            else if (e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                keyboard.PlaylistKeyPressed(keyPressed);
            }
        }

        private void ClearPlayList(object sender, RoutedEventArgs e)
        {
            mouse.ClearPlayList();
        }

        private void SortPlayList(object sender, RoutedEventArgs e)
        {
            mouse.SortPlayList();
        }

        private void AddFilesToPlayList(object sender, RoutedEventArgs e)
        {
            mouse.AddFiles();
        }

        private void OrderDefaut(object sender, RoutedEventArgs e)
        {
            mouse.OrderChange("Default");
        }

        private void OrderRepeat(object sender, RoutedEventArgs e)
        {
            mouse.OrderChange("Repeat");
        }

        private void OrderRandom(object sender, RoutedEventArgs e)
        {
            mouse.OrderChange("Random");
        }
    }
}
